package com.zy;


import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

public class ClientApp
{

    private final String host;
    private final int port;

    public ClientApp(   String server, int port){
        this.host=server;
        this.port=port;
    }

    // java -jar xxx.jar serverIP   serverPort
    public static void main( String[] args )
    {
        String server="localhost";
        int port=8083;

        if(  args!=null&&args.length==2   ){
            server=args[0];
            port=Integer.parseInt(  args[1] );
        }

        new ClientApp(   server, port     ).start();
    }

    //客户端启动方法
    private void start() {
        //配置客户端NIO线程组

        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();  // 创建Bootstrap对象, 用于启动客户端     Socket
            b.group(group)   // 绑定EventLoopGroup对象, 用于处理I/O操作的多线程事件循环
                    .channel(NioSocketChannel.class)  //客户端通道类型:  Nio socket
                    .handler(new MonitorCientChannelInitializer());     //这里的参数是一个 配置channel的类
            Channel channel = b.connect(host, port).sync().channel();

            channel.closeFuture().sync();
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            group.shutdownGracefully();
        }


        }
}
